<?php
include_once "com/code/Controller.php";
include_once "com/code/login/LoginModel.php";

class LoginController extends Controller {
	
	private $user = false;
	
	public function __construct($params) {
		parent::__construct($params);
	}
	
	public function __destruct() {
		parent::__destruct();
	}
	
	public function getLoginView($params) {
		$view = new View("com/code/login/LoginView.php");
		$view->assignController($this);
		$vars = array();
		$view->display($vars);
	}
	
	public function getUser() {
		return $this->user;
	}
	
	public function setUser($user) {
		$this->user = $user;
	}
	
	public function logged() {
		return ($this->getUser() ? true : false);
	}
	
	public function getLoggedUser($params) {
		echo "(".json_encode($this->user).")";
	}
	
	public function login($params) {
		$login = $params['login'];
		$password = $params['password'];
		$publicKey = $this->getId();
		
		$loginModel = new LoginModel();
		$user = $loginModel->getUser($login, $password, $publicKey);
		$this->setUser($user);
		
		$this->getLoggedUser($params);
	}
	
	public function logout($params) {
		$this->user = false;
		$this->getLoggedUser($params);
	}
}
?>